package com.biz.primus.ms.order.api;

import com.biz.primus.model.order.vo.request.OrderCodeAndStateVO;
import com.biz.primus.model.order.vo.request.OrderCodeVO;
import com.biz.primus.model.order.vo.request.OrderSummaryRequestVO;
import com.biz.primus.model.order.vo.request.PaidOrderRequestVO;
import com.biz.primus.model.order.vo.response.OrderResponseVO;
import com.biz.primus.model.order.vo.response.OrderSummaryResponseVO;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.order.service.impl.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前台订单接口控制器,继承BaseApiController以便统一处理捕获异常
 *
 * @author Tiger Feng
 * @date 2017年10月27日
 */
@RestController
@RequestMapping("soa/orderService")
@Api("前台订单相关接口文档")
public class OrderApiController extends BaseApiController {

    @Autowired
    private OrderServiceImpl orderService;

    @ApiOperation(value = "订单结算", notes = "计算当前订单金额")
    @PostMapping("settlement")
    public OrderSummaryResponseVO settleOrder(
            HttpServletRequest request,
            @RequestBody OrderSummaryRequestVO requestVO) {
        if (requestVO != null) {
            // 设置请求方IP地址
            requestVO.setOrderIp(request.getRemoteAddr());
        }
        return orderService.settleOrder(requestVO);
    }

    @ApiOperation("创建订单")
    @PostMapping("create")
    public OrderSummaryResponseVO createOrder(
            HttpServletRequest request,
            @RequestBody OrderSummaryRequestVO requestVO) {
        if (requestVO != null) {
            // 设置请求方IP地址
            requestVO.setOrderIp(request.getRemoteAddr());
        }
        return orderService.createOrder(requestVO);
    }

    @ApiOperation(value = "获取订单", notes = "根据订单编码获取订单")
    @PostMapping("queryByOrderCode")
    public OrderResponseVO getByOrderCode(
            @RequestBody OrderCodeVO orderCodeVO) {
        return orderService.getByOrderCode(orderCodeVO.getOrderCode());
    }


    @ApiOperation(value = "查询订单列表", notes = "根据订单类型和订单编码查询订单列表，结果以创建时间倒序")
    @PostMapping("queryListByOrderStateAndOrderCode")
    public List<OrderResponseVO> queryListByOrderStateAndOrderCode(
            @RequestBody OrderCodeAndStateVO orderCodeAndStateVO) {
        return orderService.getByOrderCodeAndState(orderCodeAndStateVO.getOrderCode(), orderCodeAndStateVO.getState());
    }

    @ApiOperation(value = "支付完成回调", notes = "订单汇总支付完成后回调通知订单更改状态")
    @PostMapping("paidOrderCallback")
    public Boolean paidOrderCallback(
            @RequestBody PaidOrderRequestVO paidOrderRequestVO) {
        return orderService.updateOrderSummary2Paid(paidOrderRequestVO);
    }

    @ApiOperation(value = "取消订单", notes = "取消指定订单编码的订单")
    @PostMapping("cancelOrder")
    public void cancelOrder(
            @RequestBody OrderCodeVO orderCodeVO) {
        orderService.cancelOrder(orderCodeVO.getOrderCode());
    }

    @ApiOperation(value = "确认收货", notes = "用户收到货物后，点击确认收货")
    @PostMapping("confirmReceipt")
    public Boolean confirmReceipt(
            @RequestBody OrderCodeVO orderCodeVO
    ) {
        return orderService.confirmReceipt(orderCodeVO.getOrderCode());
    }
}
